<?php
/**
 * 产品管理
 * @author cjli
 *
 */
class ProductAction extends HomeAction
{
	public function _initialize()
	{
		parent::_initialize();
	}
	
	public function all()
	{
		$cat_id = isset($_GET['cid']) ? intval($_GET['cid']) : 0;
		$order = isset($_GET['order']) ? $_GET['order'] : null;
		$keyword = isset($_GET['keyword']) ? trim($_GET['keyword']) : null;
		$orderby = isset($_GET['oby']) ? $_GET['oby'] : 'DESC';
		if(!in_array($orderby, array('ASC','DESC'))){
			$orderby = 'DESC';
		}
		
		$where = array(
			'is_sell' => 1,
		);
		
		if($keyword){
			$where['keyword'] = $keyword;
		}
		if($cat_id){
			$where['catid'] = $cat_id;
			$categoryInfo = CategoryModel::get_category_info($cat_id);
			$data['categoryInfo'] = $categoryInfo;
		}
	
		$order = $order ? $order : 1;
		if(in_array($order, array('1','2','3')))
		{
			if($order == '1') $where['orderby'] = 'product.id '. $orderby;
			if($order == '2') $where['orderby'] = 'product.sell_price '. $orderby;
			elseif($order == '3') $where['orderby'] = 'product.end_time '. $orderby;
		}else{
			
		}
		$data['orderby'] = $order;
		$data['oby'] = $orderby;
		
		//分页显示数
		$limit_number = C('product_list_num');
//		$searchCategoryList = ProductModel::get_product_search_category($where);
//		dump($searchCategoryList);exit;
//		//左侧分类列表并列出第个分类下的产品数
//		$leftCategoryList = CategoryModel::get_category_child_list();
//		if($leftCategoryList){
//			$cat_where = $where;
//			foreach ($leftCategoryList as &$cat){
//				$cat_where['catid'] = $cat['id'];
//				$cat['num'] = ProductModel::get_product_count($cat_where);
//				if(isset($cat['children']) && $cat['children']){
//					foreach ($cat['children'] as &$child){
//						$cat_where['catid'] = $child['id'];
//						$child['num'] = ProductModel::get_product_count($cat_where);
//						if(isset($child['children']) && $child['children']){
//							foreach ($child['children'] as &$three){
//								$cat_where['catid'] = $three['id'];
//								$three['num'] = ProductModel::get_product_count($cat_where);
//							}
//						}
//					}
//				}
//			}
//		}
//
//		dump($leftCategoryList);exit;
		//产品总数
		$productList = array();
		$productCount = 0;
		$productCount = ProductModel::get_product_count($where);
//dump($productCount);exit;
		if($productCount){
			require THEMES_PATH.'page.php';// 导入分页类
			$Page = new Page($productCount, $limit_number);// 实例化分页类 传入总记录数和每页显示的记录数
			$pageShow = $Page->show();// 分页显示输出
			$data['pageShow'] = $pageShow;
			$productList = ProductModel::get_product_list($where, $Page->firstRow, $Page->listRows);
//		}else{
//			$productList = ProductModel::get_product_list(array(), 1, $limit_number);
//			$productCount = count($productList);
		}

		$data['productCount'] = $productCount;
		$data['productList'] = $productList;
		
    $data['docTitle'] = C('meta_title');
    $data['docKeywords'] = C('meta_keywords');
    $data['docDescription'] = C('meta_description');
    
    $this->assign($data);
    unset($where['orderby']);
    $this->assign('urlArray', $where); //搜索条件
    $this->display('product_all');
	}
	
	//列表页
	public function category()
	{
		$cat_id = isset($_GET['cid']) ? intval($_GET['cid']): null;
		$recommend_id = isset($_GET['rid']) ? intval($_GET['rid']) : null;
		$brand_id = isset($_GET['bid']) ? intval($_GET['bid']) : null;
		$order = isset($_GET['order']) ? intval($_GET['order']) : null;
		$filter_attr = isset($_GET['attr']) ? $_GET['attr'] : null;
		$price_where = isset($_GET['price']) ? $_GET['price'] : null;
		$keyword = isset($_GET['keyword']) ? trim($_GET['keyword']) : null;
		$orderby = isset($_GET['oby']) ? $_GET['oby'] : 'ASC';
		if(!in_array($orderby, array('ASC','DESC'))){
			$orderby = 'ASC';
		}
		
		$where = array(
			'is_sell' => 1,
		);
		
		if($keyword){
			$where['keyword'] = $keyword;
		}
		
		if(in_array($order, array('1','2','3')))
		{
			if($order == '1') $where['orderby'] = 'id '.$orderby;
			if($order == '2') $where['orderby'] = 'sell_price '.$orderby;
			elseif($order == '3') $where['orderby'] = 'end_time '.$orderby;
		}
		
		$data['orderby'] = $order ? $order : '1';
		$data['oby'] = $orderby;
		
		//推荐分类
		if($recommend_id){
			$where['rid'] = $recommend_id;
			$recommendInfo = CategoryModel::get_category_info($recommend_id, CategoryModel::TYPE_RECOMMEND);
			$docTitle = $recommendInfo['meta_title'] ? $recommendInfo['meta_title'] : $recommendInfo['name'];
    	$docKeywords = $recommendInfo['docKeywords'];
    	$docDescription = $recommendInfo['docDescription'];
    	$data['recommendInfo'] = $recommendInfo;
		}
		
		//品牌
		if($brand_id){
			$where['bid'] = $brand_id;
			$brandInfo = CategoryModel::get_category_info($brand_id, CategoryModel::TYPE_BRAND);
			$docTitle = $brandInfo['meta_title'] ? $brandInfo['meta_title'] : $brandInfo['name'];
    	$docKeywords = $brandInfo['docKeywords'];
    	$docDescription = $brandInfo['docDescription'];
    	$data['brandInfo'] = $brandInfo;
		}
		
		
		//产品分类
		if($cat_id){
			$where['catid'] = $cat_id;
			$searchCategoryInfo = $categoryInfo = CategoryModel::get_category_info($cat_id);
			$model_id = $categoryInfo['model_id'];
    	$docTitle = $categoryInfo['meta_title'] ? $categoryInfo['meta_title'] : $categoryInfo['name'];
    	$docKeywords = $categoryInfo['meta_keywords'];
    	$docDescription = $categoryInfo['meta_description'];
    	$data['categoryInfo'] = $categoryInfo;
		}
/**
		//筛选属性处理
		$attributeList = array();
		if($searchCategoryInfo['filter_attr']>0)
		{
			$where['attr'] = $filter_attr;
			
				$filter_attr = explode('.', $filter_attr);
    	 	$cat_filter_attr = explode(',', $categoryInfo['filter_attr']);       //提取出此分类的筛选属性
        $all_attr_list = array();
        if($searchCategoryInfo['vid'] == CategoryModel::TYPE_CATEGORY){
        	$all_cat_ids = CategoryModel::get_category_child_ids($searchCategoryInfo['id']);
        }else{
        	$all_cat_ids = $searchCategoryInfo['id'];
        }
        
				//循环筛选属性
        foreach ($cat_filter_attr as $key => $attr_id)
        {
        	$attribute = AttributeModel::get_attribute_info($attr_id);
        	if(empty($attribute)){ 
        		continue;
        	}
        	
        	$all_attr_list[$key]['filter_attr_name'] = $attribute['attr_name'];
        	
        	//属性值列表
        	$attr_values_list = AttributeModel::get_attribute_values_list($attr_id);
					
        	//属性与产品关联列表
        	$attr_list = ProductModel::get_attribute_search_product_list($attr_id, $all_cat_ids);

					if(empty($attr_list)) continue;

					$temp_arrt_url_arr = array();

          for ($i = 0; $i < count($cat_filter_attr); $i++)        //获取当前url中已选择属性的值，并保留在数组中
          {
          	$temp_arrt_url_arr[$i] = !empty($filter_attr[$i]) ? $filter_attr[$i] : 0;
          }
         
        	$temp_arrt_url_arr[$key] = 0;                           //“全部”的信息生成
          $temp_arrt_url = implode('.', $temp_arrt_url_arr);
          //初始化
          $all_attr_list[$key]['attr_list'][0]['attr_value'] = L('_ALL_ATTRIBUTE_');
          $all_attr_list[$key]['attr_list'][0]['url'] = url('category', array('cid'=>$cat_id, 'bid'=>$brand_id, 'price'=>$price_where, 'rid' => $recommend_id, 'attr'=>$temp_arrt_url));
          $all_attr_list[$key]['attr_list'][0]['selected'] = empty($filter_attr[$key]) ? 1 : 0;

          if($attr_list){
	          foreach ($attr_list as $k => $v)
	          {
	          	if(empty($v['attr_value']) && $v['attr_value_id']){
	          		$v['attr_value'] = $attr_values_list[$v['attr_value_id']]['attr_value'];
	          		$v['attr_img'] = $attr_values_list[$v['attr_value_id']]['attr_img'];
	          	}
	          	$temp_key = $k + 1;
	            $temp_arrt_url_arr[$key] = $v['goods_id'];       //为url中代表当前筛选属性的位置变量赋值,并生成以‘.’分隔的筛选属性字符串
	            $temp_arrt_url = implode('.', $temp_arrt_url_arr);
	
	            $all_attr_list[$key]['attr_list'][$temp_key]['attr_value'] = $v['attr_value'];
	            $all_attr_list[$key]['attr_list'][$temp_key]['url'] = url('category', array('cid'=>$cat_id, 'bid'=>$brand_id, 'rid' => $recommend_id, 'price'=>$price_where, 'attr'=>$temp_arrt_url));
	            $all_attr_list[$key]['attr_list'][$temp_key]['attr_img'] = isset($v['attr_img']) ? $v['attr_img'] : null;
	
	            if (!empty($filter_attr[$key]) AND $filter_attr[$key] == $v['goods_id'])
	            {
	            	$all_attr_list[$key]['attr_list'][$temp_key]['selected'] = 1;
	            }
	            else
	            {
	            	$all_attr_list[$key]['attr_list'][$temp_key]['selected'] = 0;
	            }
	         	}
					}
        }
        
    	}
    	$this->assign('attributeList', isset($all_attr_list) ? $all_attr_list : null);
//dump($all_attr_list);exit;
*/

		//分页显示数
		$limit_number = C('product_list_num');
		
		//价格
		if($price_where){
			$where['price'] = $price_where;
		}


		//产品总数
		$productList = array();
		$productCount = 0;
		$productCount = ProductModel::get_product_count($where);

//		if($productCount){
//			require THEMES_PATH.'page.php';// 导入分页类
//			$Page = new Page($productCount, $limit_number);// 实例化分页类 传入总记录数和每页显示的记录数
//			$pageShow = $Page->show();// 分页显示输出
//			$data['pageShow'] = $pageShow;
//			$productList = ProductModel::get_product_list($where, $Page->firstRow, $Page->listRows);
////		}else{
////			if(isset($where['attr'])){
////				unset($where['attr']);
////			}
////			$productList = ProductModel::get_product_list($where, 0, $limit_number);
////			$productCount = count($productList);
//		}
		$productList = ProductModel::get_product_list($where);
		$productNumber = 0;
		$productNumber = OrderModel::get_product_count();//获取这周的销售数量
		for($i=4;$i>=0;$i--){
			if(substr($productNumber,$i,1) > 0)
				$productSum[] = substr($productNumber,$i,1);
			else
				$productSum[] = 0;
			
		}
		$data['productSum'] = $productSum;
		$data['productCount'] = $productCount;
		$data['productList'] = $productList;
		
		//相关产品
//		$filter = array(
//			'catid' => $cat_id,
//			'rid' => $recommend_id,
//			'bid' => $brand_id,
//		);
//		$similarProductList = ProductModel::get_random_product_list('similar_product', $filter, 8);
//		$data['similarProductList'] = $similarProductList;

		
    
/**    //热销产品
    $filter = array(
    	'rid' => 13,
    	'catid' => $cat_id ? $cat_id : null,
    );
    $hotProductList = ProductModel::get_product_list($filter, 0, 20);
    $data['hotProductList'] = $hotProductList;
*/    
    
    //最高价格最低价格
//    $priceLimits = ProductModel::get_product_price_limit($where);
//    $data['priceLimits'] = $priceLimits;

		$data['docTitle'] = $docTitle;
    $data['docKeywords'] = $docKeywords;
    $data['docDescription'] = $docDescription;
    
    //当前路径
    $pathArray = array();
    if(isset($categoryInfo) && $categoryInfo){
    	$pathArray = CategoryModel::get_category_pos($categoryInfo['id']);
    }

     $this->assign('pathArray', $pathArray);
     unset($where['orderby']);
    $this->assign('urlArray', $where); //搜索条件
		$this->assign($data);
		$this->display('productlist');
	}
	
	
	//产品详细页
	public function info()
	{
		$pid = intval($_GET['id']);
		if(!$pid){
			redirect(url(''));
		}
		//产品信息
		$product = ProductModel::get_product_info($pid);
		if(!$product){
			redirect(url(''));
		}
		
		//更新产品访问量
		ProductModel::update_product_visit($pid);
		
		//产品分类
		$categoryInfo = CategoryModel::get_category_info($product['catid']);
		$data['categoryInfo'] = $categoryInfo;
		//品牌
		$brandInfo = CategoryModel::get_category_info($product['brand_id'], CategoryModel::TYPE_BRAND);
		$data['brandInfo'] = $brandInfo;
		
		//产品属性
		$properties = ProductModel::get_product_relation_attribte_list($pid, array('attr_sell' => 1));// 获得商品的规格和属性
		$this->assign('properties', isset($properties['pro']) ? $properties['pro'] : null);                              // 商品属性
    	$this->assign('specification', isset($properties['spe']) ? $properties['spe'] : null);                              // 商品规格
//		dump($properties);exit;
    //图库
		$product['photoList'] = ProductModel::get_product_photo_list($pid);
		
		//产品批发
//		$product['wholesale'] = PromotionModel::get_product_wholesale_info($pid);
		
		
		//相关产品
			$product['relationList'] = ProductModel::get_product_relation_list($pid, 1, 4);	
		//同类产品
		$similarProductList = ProductModel::get_random_product_list('similar_product', array('catid' => $categoryInfo['id']), 12);
		$data['similarProductList'] = $similarProductList;
		//所有产品
//		$allproductList = ProductModel::get_random_product_list('all_product', array('catid' => $categoryInfo['id'], 'level' => 1), 8);
//		$data['allproductList'] = $allproductList;
		//评论
		$commentlist = CommentModel::get_product_comment_list($pid);
		$data['commentlist'] = $commentlist;
		
		//是否显示评论入口
		$comment_display = C('comment_display');
		//开启
		if($comment_display){
			global $user;
   		$comment_type = C('comment_type');
   		if($comment_type==1 && !UserModel::logged()){
   			$comment_display = 0;
   		}
		}
		$this->assign('comment_display', $comment_display);
		
		if(!isset($_SESSION['comment_token']) || empty($_SESSION['comment_token'])){
	     import('ORG.Util.String');
	     $_SESSION['comment_token'] = String::randString(16);
     }
     $this->assign('comment_token',  $_SESSION['comment_token']);
	
	//先查询是否审核已开启
	$shaidan_disabled = C('shaidan_disabled');		
	//晒单
	$shaidanlist = ProductModel::get_shaidan_list($pid,$shaidan_disabled);
	//print_r($shaidanlist);
	if($shaidanlist) {
		for($i=0;$i<count($shaidanlist);$i++) {
			//关联图片 
			$sid = $shaidanlist[$i]['id'];
			$photoList02 = ProductModel::getShaidanPhotoList($shaidanlist[$i]['id']);
			$photoList[$sid] = $photoList02;
			
		}
		$this->assign('photoList', $photoList);
	}
	$data['shaidanlist'] = $shaidanlist;
		
	//页面meta
	$data['docTitle'] = $product['meta_title'] ? $product['meta_title'] : $product['name'];
    $data['docKeywords'] = $product['meta_keywords'];
    $data['docDescription'] = $product['meta_description'];
		
		//当前路径
    $pathArray = array();
    if(isset($categoryInfo) && $categoryInfo){
    	$pathArray = CategoryModel::get_category_pos($product['catid']);
    }
     $this->assign('pathArray', $pathArray);
		$data['productInfo'] = $product;
		
		$this->assign($data);
		/* 记录浏览历史 */
//		$this->history($pid);
		
		$this->display('product');
	}
	
	/**
	 * 初级定制
	 */
	public function custom()
	{
		$pid = intval($_GET['pid']);
		$productInfo = ProductModel::get_product_info($pid);
		if(!$productInfo){
			$this->error(L('_PRODUCT_NO_EXIST_'), url(''));
		}
		
		$data['productInfo'] = $productInfo;
		
		$this->assign($data);
		$this->display('product_custom');
	}
	
	/**
	 * 高级定制
	 */
	public function advanced_custom()
	{
		$pid = intval($_GET['pid']);
		$productInfo = ProductModel::get_product_info($pid);
		if(!$productInfo){
			$this->error(L('_PRODUCT_NO_EXIST_'), url(''));
		}
		
		$data['productInfo'] = $productInfo;
		
		$this->assign($data);
		$this->display('product_advanced');
	}
	
	//写评论
	public function comment()
	{
		$pid = intval($_GET['pid']);
		$productInfo = ProductModel::get_product_info($pid);
		if(!$productInfo){
			$this->error(L('_PRODUCT_NO_EXIST_'), url(''));
		}
		//是否显示评论入口
		$comment_display = C('comment_display');
		//开启
		if($comment_display){
			global $user;
   		$comment_type = C('comment_type');
   		if($comment_type==1 && !UserModel::logged()){
   			$comment_display = 0;
   		}
		}
		if(!$comment_display){
			$this->error(L('_ILLEGAL_OPERATION_'), url('product/info', 'id='.$pid));
		}
		//提交
		if(isset($_POST['dosubmit']))
		{
			if($_POST['comment_token'] != $_SESSION['comment_token'])
			{
				$this->error(L('_OPERATION_FAIL_'), url('product/comment', 'pid='.$pid));
			}
			if(empty($_POST['username']) || empty($_POST['email']) || empty($_POST['title']) || empty($_POST['content'])){
				$this->error(L('_PLEASE_INPUT_CORRECT_OCCUPATIONAL_INFORMATION_'), url('product/comment', 'pid='.$pid));
			}
			
			
			$_POST['product_id'] = $pid;
			if(CommentModel::insert_comment($_POST)){
				$this->success(L('_OPERATION_SUCCESS_'), url('product/info', 'id='.$pid));
			}else{
				$this->error(L('_OPERATION_FAIL_'), url('product/comment', 'pid='.$pid));
			}
		}else{
		//当前路径
    $pathArray = CategoryModel::get_category_pos($productInfo['catid']);
     $this->assign('pathArray', $pathArray);
     
     if(!isset($_SESSION['comment_token']) || empty($_SESSION['comment_token'])){
	     import('ORG.Util.String');
	     $_SESSION['comment_token'] = String::randString(16);
     }
     $this->assign('comment_token',  $_SESSION['comment_token']);
     
		
			$this->assign('productInfo', $productInfo);
			$this->display('comment');
		}
	}
	
	public function comment_ajax()
	{
		global $user;
		$pid = intval($_GET['pid']);
		$productInfo = ProductModel::get_product_info($pid);
		if(!$productInfo){
			echo L('_PRODUCT_NO_EXIST_');exit;
		}
		//是否显示评论入口
		$comment_display = C('comment_display');
		//开启
		if($comment_display){
   		$comment_type = C('comment_type');
   		if($comment_type==1 && !UserModel::logged()){
   			$comment_display = 0;
   		}
		}
		if(!$comment_display){
			echo L('_ILLEGAL_OPERATION_');exit;
		}
			
		//提交
		if(IS_POST)
		{
			$post = $_POST;
			if($post['comment_token'] != $_SESSION['comment_token'])
			{
				echo L('_OPERATION_FAIL_');exit;
			}
			if(empty($post['username']) || empty($post['email']) || empty($post['title']) || empty($post['content'])){
				echo L('_PLEASE_INPUT_CORRECT_OCCUPATIONAL_INFORMATION_');exit;
			}
			
			
			$post['product_id'] = $pid;
			if(CommentModel::insert_comment($post)){
				echo L('_OPERATION_SUCCESS_');exit;
			}else{
				echo L('_OPERATION_FAIL_');exit;
			}
		}
		echo L('_ILLEGAL_OPERATION_');exit;
	}
	
	public function search()
	{
		$keyword = trim($_GET['keyword']);
		$where = array();
		if($keyword){
			$where['name'] = $keyword;
		}
		
		//分页显示数
		$limit_number = C('product_list_num');
		
		//产品总数
		$productCount = ProductModel::get_product_count($where);
		if($productCount){
			import("ORG.Util.Page");// 导入分页类
			$Page = new Page($productCount, $limit_number);// 实例化分页类 传入总记录数和每页显示的记录数
			$pageShow = $Page->show();// 分页显示输出
			$data['pageShow'] = $pageShow;
			$productList = ProductModel::get_product_list($where, $Page->firstRow, $Page->listRows);
		}else{
			$productList = ProductModel::get_product_list(array(), 0, 20);
			$productCount = count($productList);
		}
		$data['productCount'] = $productCount;
		$data['productList'] = $productList;
    
    //热销产品
    $filter = array(
    	'rid' => 13,
    );
    $hotProductList = ProductModel::get_product_list($filter, 0, 20);
    $data['hotProductList'] = $hotProductList;
    
    //品牌列表
    $brandsList = CategoryModel::get_category_list_base(CategoryModel::TYPE_BRAND);
    $data['brandsList'] = $brandsList;
    
    $this->assign($data);
     $this->assign('keyword', $keyword); //搜索条件
		$this->display('product_search');
	}
	
	//获取商品价格
	public function getPrice()
	{
		global $user;
		$res = array(
			'error' => 0,
			'message' => null,
		);
		$post =(array)json_decode($_POST['post']);
		$pid = intval($post['pid']);
		$attr = $post['attr'];
		$res['qty'] = $qty = intval($post['qty']) ? intval($post['qty']) : 1;

		$productInfo = ProductModel::get_product_info($pid);
		if(!$productInfo){
			$res['message'] = L('_PRODUCT_NO_EXIST_');
			$res['error'] = 1;
		}else{
			$price = ProductModel::get_final_price($pid, $qty, true, $attr);
			$res['message'] = currency($price*$qty);
		}
		echo json_encode($res);
	}
	
	/**
	 * 获取产品属性
	 */
	public function attribute_ajax()
	{
		global $user;
		$res = array(
			'error' => 0,
			'message' => null,
		);
		$pid = intval($_REQUEST['pid']);
//		$pid = 195200;
		$productInfo = ProductModel::get_product_info($pid);
		if(!$productInfo){
			$res['message'] = L('_PRODUCT_NO_EXIST_');
			$res['error'] = 1;
			
		}else{
			$attributeList = ProductModel::get_product_relation_attribte_list($pid);
			if($attributeList && isset($attributeList['spe'])){
				$res['error'] = -1;
				$res['message'] = $attributeList['spe'];
				$res['content'] = $productInfo['name'];
			}
			//没有属性
			else{
				$cartAction = A('Cart');
				$res = $cartAction->add_product_cart($pid,1,null,true);
			}
		}
		echo json_encode($res);
	}
	
	/* 记录浏览历史 */
	private function history($goods_id)
	{
		$cook_exp = array(
		'expire' => time() + 3600 * 24 * 30
		);
		if (cookie('think_rongpai_history'))
		{
		    $history = explode(',', cookie('think_rongpai_history'));

		    array_unshift($history, $goods_id);
		    $history = array_unique($history);
		
		    while (count($history) > 5)
		    {
		        array_pop($history);
		    }
		
		    cookie('think_rongpai_history',implode(',', $history), $cook_exp);
		}
		else
		{
			cookie('think_rongpai_history',$goods_id, $cook_exp);
		}
	}
	/*清空浏览历史*/
	public function clear_history()
	{
		$cook_exp = array(
		'expire' => time() + 3600 * 24 * 30
		);
		cookie('think_rongpai_history',NULL, $cook_exp);
	}
	
	//产品详细页
	public function presell()
	{
		$pid = intval($_GET['id']);
		if(!$pid){
			redirect(url(''));
		}
		//产品信息
		$product = ProductModel::get_product_info($pid);
		if(!$product){
			redirect(url(''));
		}
		/*$value = '4_0';
		$arr = explode('_', $value);
		echo count($arr);
		print_r($arr);
		$arr[count($arr)-1];*/
		
		//更新产品访问量
		ProductModel::update_product_visit($pid);
		
		//产品分类
		$categoryInfo = CategoryModel::get_category_info($product['catid']);
		$data['categoryInfo'] = $categoryInfo;
		
    	//图库
		$product['photoList'] = ProductModel::get_product_photo_list($pid);
		
		//相关产品
		$product['relationList'] = ProductModel::get_product_relation_list($pid, 1, 4);	
		
		//页面meta
		$data['docTitle'] = $product['meta_title'] ? $product['meta_title'] : $product['name'];
		$data['docKeywords'] = $product['meta_keywords'];
		$data['docDescription'] = $product['meta_description'];
		

		$data['productInfo'] = $product;
		
		$this->assign($data);

		
		$this->display('product_presell');
	}
	
	/**
	 * 获取用户选的尺码
	 */
	public function getProductSize()
	{
		$price = 0;
		$product_id = intval($_GET['product_id']);
		$id = intval($_GET['id']);
		if($product_id==0){
			exit(0);
		}
		$product_size = PromotionModel::get_product_size_info($product_id);
		$prices = unserialize($product_size['prices']);
		$prices_size = $prices[$id];
		//统计该尺码的销售数量
		$order_item_db = D('orders_items');
		$order_item_db->field('sum(qty) as sum');
		$order_item_db->where('product_id='.$product_id.' AND chima="'.$prices_size['size'].'"');
		$productSum = $order_item_db->find();
		//$prices_size['qty'] = $prices_size['qty']-$productSum['sum'];
		//$prices_size['num'] = $productSum['sum'];
		if($productSum['sum'] > 0)
			$prices_size['num'] = $productSum['sum'];
		else
			$productSum['sum'] = 0;
		if(isset($prices_size['size'])){
			$size = array(
				'size' 	=> trim($prices_size['size']),
				'qty' 	=> $prices_size['qty'],
				'stock' => $prices_size['stock'],
				'num' 	=> $prices_size['num'],
			);
		}
		else{
			exit(0);
		}
		echo json_encode($size);exit;
	}
}